package com.dave.admin.component;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.dave.admin.common.base.BaseEntity;
import com.dave.admin.util.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * Mybatis-plus 自动填充器
 * <p>
 *
 * @author Dave
 */
@Component
@Slf4j
public class BossMetaObjectHandler implements MetaObjectHandler {


    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        this.strictInsertFill(metaObject, BaseEntity.CREATOR, String.class, SecurityUtil.getUserAdmin().getNickName());
        this.strictInsertFill(metaObject, BaseEntity.CREATE_TIME, LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, BaseEntity.UPDATER, String.class, SecurityUtil.getUserAdmin().getNickName());
        this.strictInsertFill(metaObject, BaseEntity.UPDATE_TIME, LocalDateTime.class, LocalDateTime.now());
    }


    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        this.strictUpdateFill(metaObject, BaseEntity.UPDATER, String.class, SecurityUtil.getUserAdmin().getNickName());
        this.strictUpdateFill(metaObject, BaseEntity.UPDATE_TIME, LocalDateTime.class, LocalDateTime.now());
    }
}
